
function getQueryString() {
  let name, value;
  let str = location.href; //取得整个地址栏
  str = str.replace(/\?from.*?#\//g,'#/');
  let num = str.indexOf("?")
  str = str.substr(num + 1); //取得所有参数
  let arr = str.split("&"); //各个参数放到数组里
  for (let i = 0; i < arr.length; i++) {
    num = arr[i].indexOf("=");
    if (num > 0) {
      name = arr[i].substring(0, num).toLowerCase();
      value = arr[i].substr(num + 1);
      this[name] = value;
    }
  }
}

/**
 * 存储localStorage
 */
const setStore = (name, content) => {
  if (!name) return;
  if (typeof content !== 'string') {
    content = JSON.stringify(content);
  }
  window.localStorage.setItem(name, content);
}

/**
 * 获取localStorage
 */
const getStore = name => {
  if (!name) return;
  const dataStore = window.localStorage.getItem(name);
  try {  
    if (typeof JSON.parse(dataStore) == "object") {  
      return JSON.parse(dataStore);
    }  
  }catch(e) {  
    return dataStore;
  }  
  return dataStore;
}

/**
 * 删除localStorage
 */
const removeStore = name => {
  if (!name) return;
  window.localStorage.removeItem(name);
}

//sessionStorage
const setSession = (name, content) => {
  if (!name) return;
  if (typeof content !== 'string') {
    content = JSON.stringify(content);
  }
  sessionStorage.setItem(name, content);
}

/**
 * sessionStorage
 */
const removeSession = name => {
  if (!name) return;
  window.sessionStorage.removeItem(name);
}

const getSession = name => {
  if (!name) return;
  const dataStore = sessionStorage.getItem(name);
  try {  
    if (typeof JSON.parse(dataStore) == "object") {  
      return JSON.parse(dataStore);
    }  
  }catch(e) {  
    return dataStore;
  }  
  return dataStore;
}

const transTime = (S,AM) => {
  let T = new Date(1E3 * S),
      Format = Q => {return Q < 10 ? '0' + Q : Q},
      result = Format(T.getHours()) + ':' + Format(T.getMinutes());
  let oneDay = 60 * 60 * 24;//s
  let zeroTime = new Date().setHours(0,0,0,0)/1000;
  let passTwo = zeroTime - oneDay;//昨天凌晨
  if(S > passTwo && S < zeroTime){
    result ='昨天 ' + result;
  }else if(S < zeroTime){
    let month = T.getMonth() + 1,
        days = T.getDate();
        result = month + '月' + days + '日 ' + result;
  }else{
    if(typeof AM !== 'undefined'){
      let tHour = T.getHours(),
          tDetail = tHour > 12 ? '下午 ':'上午 ';
      result = tDetail + result;
    }
  }
  return result;
}

const formatData = Q =>{
  return Q < 10 ? '0' + Q : Q;
};

const isIOS = () =>{ 
  let u = navigator.userAgent, app = navigator.appVersion;   
  //let isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1; //android终端或者uc浏览器   
  return !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端   
}
const setCookie = (c_name,value,expiretime) => {
  /*当前日期*/
  var today = new Date();
  /*Cookie过期时间*/
  var expire = new Date();
  /*如果未设置nDays参数或者nDays为0，取默认值1*/
  //if(nDays == null || nDays == 0) nDays = 1;
  /*计算Cookie过期时间【 3600000 * 24  为一天】*/
  expire.setTime(today.getTime() + expiretime); //1小时
    document.cookie=c_name+ "=" +escape(JSON.stringify(value))+
      ((expiretime==null) ? "" : ";expires="+expire.toGMTString())
};

const getCookie = c_name => {
  if (document.cookie.length>0)
  {
    var  c_start=document.cookie.indexOf(c_name + "=")
    if (c_start!=-1)
    {
      c_start=c_start + c_name.length+1
     var c_end=document.cookie.indexOf(";",c_start)
      if (c_end==-1) c_end=document.cookie.length
      return JSON.parse(unescape(document.cookie.substring(c_start,c_end)));
    }
  }
  return ""
};


const utf8ByteToUnicodeStr = utf8Bytes => {
  var unicodeStr ="";
  for (var pos = 0; pos < utf8Bytes.length;){
      var flag= utf8Bytes[pos];
      var unicode = 0 ;
      if ((flag >>>7) === 0 ) {
          unicodeStr+= String.fromCharCode(utf8Bytes[pos]);
          pos += 1;

      } else if ((flag &0xFC) === 0xFC ){
          unicode = (utf8Bytes[pos] & 0x3) << 30;
          unicode |= (utf8Bytes[pos+1] & 0x3F) << 24;
          unicode |= (utf8Bytes[pos+2] & 0x3F) << 18;
          unicode |= (utf8Bytes[pos+3] & 0x3F) << 12;
          unicode |= (utf8Bytes[pos+4] & 0x3F) << 6;
          unicode |= (utf8Bytes[pos+5] & 0x3F);
          unicodeStr+= String.fromCharCode(unicode) ;
          pos += 6;

      }else if ((flag &0xF8) === 0xF8 ){
          unicode = (utf8Bytes[pos] & 0x7) << 24;
          unicode |= (utf8Bytes[pos+1] & 0x3F) << 18;
          unicode |= (utf8Bytes[pos+2] & 0x3F) << 12;
          unicode |= (utf8Bytes[pos+3] & 0x3F) << 6;
          unicode |= (utf8Bytes[pos+4] & 0x3F);
          unicodeStr+= String.fromCharCode(unicode) ;
          pos += 5;

      } else if ((flag &0xF0) === 0xF0 ){
          unicode = (utf8Bytes[pos] & 0xF) << 18;
          unicode |= (utf8Bytes[pos+1] & 0x3F) << 12;
          unicode |= (utf8Bytes[pos+2] & 0x3F) << 6;
          unicode |= (utf8Bytes[pos+3] & 0x3F);
          unicodeStr+= String.fromCharCode(unicode) ;
          pos += 4;

      } else if ((flag &0xE0) === 0xE0 ){
          unicode = (utf8Bytes[pos] & 0x1F) << 12;;
          unicode |= (utf8Bytes[pos+1] & 0x3F) << 6;
          unicode |= (utf8Bytes[pos+2] & 0x3F);
          unicodeStr+= String.fromCharCode(unicode) ;
          pos += 3;

      } else if ((flag &0xC0) === 0xC0 ){ //110
          unicode = (utf8Bytes[pos] & 0x3F) << 6;
          unicode |= (utf8Bytes[pos+1] & 0x3F);
          unicodeStr+= String.fromCharCode(unicode) ;
          pos += 2;

      } else{
          unicodeStr+= String.fromCharCode(utf8Bytes[pos]);
          pos += 1;
      }
  }
  return unicodeStr;
}

const Client = {
  getQueryString,
  setStore,
  getStore,
  removeStore,
  transTime,
  isIOS,
  formatData,
  setSession,
  getSession,
  removeSession,
  setCookie,
  getCookie,
  utf8ByteToUnicodeStr

}
export default Client